﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphEditor;
using SharedUtils;
using SceneEditor;
using System.Xml.Serialization;
using System.IO;

namespace Editor
{
    public partial class Editor : Form
    {

        SceneEditorForm scene_editor;
        GraphEditorForm graph_editor;
        public Util utils = new Util();

        public const string RootFileLocation = @"C:\Program Files\PictureSense";
        public string ProjectName = "NoName";


        public Editor()
        {
            this.Text = ProjectName;
            if(!Directory.Exists(RootFileLocation))
            {
                Directory.CreateDirectory(RootFileLocation);
            }

            InitializeComponent();
            scene_editor = new SceneEditorForm(utils);
            graph_editor = new GraphEditorForm(this.toolStripStatusLabel1, utils, new Graph<SharedUtils.Node, EdgeData>(true, true));
            

            this.tabPage1.Controls.Add(scene_editor);
            this.tabPage2.Controls.Add(graph_editor);


            this.tabPage1.Text = "Scene Editor";
            this.tabPage2.Text = "Graph Editor";
            graph_editor.Size = tabPage2.Size;

            saveFileDialog1.InitialDirectory = RootFileLocation;
            openFileDialog1.InitialDirectory = RootFileLocation;
        }

        protected override void OnLoad(EventArgs e)
        {
            this.Text = ProjectName;
            base.OnLoad(e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (graph_editor != null)
                graph_editor.Size = tabPage2.Size;
        }

        void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 1)
                graph_editor.Resolve();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string writeLocation = Path.Combine(RootFileLocation, ProjectName);
            if(!Directory.Exists(writeLocation))
            {
                Directory.CreateDirectory(writeLocation);
            }

            string[] files = Directory.GetFiles(writeLocation);
            foreach (var item in files)
            {
                File.Delete(item);
            }
                

            foreach (var item in utils.entities)
            {
                if(item is PictureEntity)
                {
                    PictureEntity pe = (PictureEntity)item;
                    string path = pe.picturePath;
                    string filename = Path.GetFileName(path);
                    File.Copy(path, Path.Combine(writeLocation, filename));
                    path = Path.Combine(writeLocation, filename);
                    pe.picturePath = path;
                }
                if(item is SoundEntity)
                {
                    SoundEntity pe = (SoundEntity)item;
                    //Play
                    string path = pe.playPath;
                    string filename = Path.GetFileName(path);
                    File.Copy(path, Path.Combine(writeLocation, filename));
                    path = Path.Combine(writeLocation, filename);
                    pe.playPath = path;

                    //Pause
                    path = pe.pausePath;
                    filename = Path.GetFileName(path);
                    File.Copy(path, Path.Combine(writeLocation, filename));
                    path = Path.Combine(writeLocation, filename);
                    pe.pausePath = path;

                    //Stop
                    path = pe.stopPath;
                    filename = Path.GetFileName(path);
                    File.Copy(path, Path.Combine(writeLocation, filename));
                    path = Path.Combine(writeLocation, filename);
                    pe.stopPath = path;

                    //Sound
                    path = pe.soundPath;
                    filename = Path.GetFileName(path);
                    File.Copy(path, Path.Combine(writeLocation, filename));
                    path = Path.Combine(writeLocation, filename);
                    pe.soundPath = path;
                }
            }

            XmlSerializer serializer = new XmlSerializer(typeof(Util));
            TextWriter textWriter = new StreamWriter("temp_xml");
            serializer.Serialize(textWriter, utils);
            textWriter.Close();

            File.Copy("temp_xml", Path.Combine(writeLocation, "PresentationXML.xml"));
            this.Text = ProjectName;
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = saveFileDialog1.ShowDialog();
            if(dr == DialogResult.OK)
            {
                //Set the new Project Name
                string filename = saveFileDialog1.FileName;
                string[] dirs = filename.Split('\\');
                filename = dirs[dirs.Length-1];
                ProjectName = filename;

                //Save it out.
                saveToolStripMenuItem_Click(sender, e);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if(dr == DialogResult.OK)
            {
                string filepath = openFileDialog1.FileName;

                XmlSerializer serializer = new XmlSerializer(typeof(Util));

                StreamReader reader = new StreamReader(filepath);
                utils = (Util)serializer.Deserialize(reader);
                reader.Close();
            }

            utils.defaultCamera = utils.cameras[0];
            scene_editor.Deserialize(utils);
            this.Text = ProjectName;
        }
    }
}
